package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.mapper.DishMapper;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

////    static  long id =68;
//    @Autowired
//    private DishMapper dishMapper;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> Upload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
        UUID uuid = UUID.randomUUID();
        String ObjectName = uuid.toString() + extension;
        try {
            String uploadPath = aliOssUtil.upload(file.getBytes(), ObjectName);
            return  Result.success(uploadPath);
        } catch (IOException e) {
            log.info("文件上传失败：{}",e);
        }
        return  Result.error(MessageConstant.UPLOAD_FAILED);
    }

}
